/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import java.awt.Color;
import net.worlds.WorldsException;
import net.worlds.PropChangeHandler;
import net.worlds.Util;
import se.krka.kahlua.vm.JavaFunction;
import se.krka.kahlua.vm.LuaCallFrame;


public class Environment extends GameObject{
    public Color fogColor;
    public int fogAmount;
    public float fracTime;
    private int hours;
    private int mins;
    public Environment() throws WorldsException
    {
        super("Environment");
        addPropChangeHandler("FogColor", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                fogColor = (Color)value;
            }
        });
        addPropChangeHandler("FogAmount", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                fogAmount = (int) ((Float)value * 255);
            }
        });
        addPropChangeHandler("Time", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                String s = value.toString();
                boolean valid = true;
                if(s.contains(":"))
                {
                    String[] parts = s.split(":");
                    if(Util.isInteger(parts[0]) && Util.isInteger(parts[1]))
                    {
                        int h = Integer.parseInt(parts[0]);
                        int m = Integer.parseInt(parts[1]);
                        hours = h;
                        mins = m;
                        if(h > -1 && h < 24 && m > - 1 && m < 60)
                            setTime(h, m);
                        else
                            valid = false;
                    }
                    else
                        valid = false;
                }
                else
                    valid = false;
                if(!valid)
                    throw new RuntimeException("Invalid time");
            }
        });
        addFunction("step", new JavaFunction()
        {
            public int call(LuaCallFrame callFrame, int nArguments) {
                try {
                    mins++;
                    if (mins >= 60) {
                        mins = 0;
                        hours++;
                        if (hours >= 24) {
                            hours = 0;
                            mins = 0;
                        }
                    }
                    setProp("Time", hours + ":" + mins, false);
                    setTime(hours, mins);
                    return 0;
                } catch (WorldsException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        //x_;
    }
    private void setTime(int h, int m)
    {
        fracTime = h/(float)24 + (m/(float)60)/24;
    }
}
